/**
 * 通用js
 */

//全局变量
var SITE_URL="http://"+window.location.host+"/";
var STATIC_URL=SITE_URL;
var IMAGE_URL=SITE_URL;
var FILE_URL=SITE_URL;

// 添加Cookie
function addCookie(name, value, options, doc) {
	if (arguments.length > 1 && name != null) {
		if (options == null) {
			options = {};
		}
		if (value == null) {
			options.expires = -1;
		}
		if (typeof options.expires == "number") {
			var time = options.expires;
			var expires = options.expires = new Date();
			expires.setTime(expires.getTime() + time * 1000);
		}
		(doc || document).cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
	}
}

// 获取Cookie
function getCookie(name, doc) {
	if (name != null) {
		var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec((doc || document).cookie);
		return value ? decodeURIComponent(value[1]) : null;
	}
}

// 移除Cookie
function removeCookie(name, options) {
	addCookie(name, null, options);
}

(function($) {
	
	// 令牌	
	$(document).ajaxSend(function(event, request, settings) {
		if (!settings.crossDomain && settings.type != null && settings.type.toLowerCase() == "post") {
			var token = getCookie("token");
			if (token != null) {
				request.setRequestHeader("token", token);
			}
		}
	});
	
	$(document).ajaxComplete(function(event, request, settings) {
		try {
			if(typeof request === 'function'){
				var loginStatus = request.getResponseHeader("loginStatus");
				var tokenStatus = request.getResponseHeader("tokenStatus");
				
				if (loginStatus == "accessDenied") {
					$.redirectLogin(location.href, "服务端拒绝访问");
				} else if (tokenStatus == "accessDenied") {
					var token = getCookie("token");
					if (token != null) {
						$.extend(settings, {
							global: false,
							headers: {token: token}
						});
						$.ajax(settings);
					}
				}
			}
		} catch (e) {
			console.log(e);
		}
	});

})(jQuery);

// 令牌
$().ready(function() {

	$("form").submit(function() {
		var $this = $(this);
		if ($this.attr("method") != null && $this.attr("method").toLowerCase() == "post" && $this.find("input[name='token']").size() == 0) {
			var token = getCookie("token");
			if (token != null) {
				$this.append('<input type="hidden" name="token" value="' + token + '" \/>');
			}
		}
	});
	
	$(".loading").click(function(){
		var _href = $(this).attr("_href");
		 layer.load(0,{share:false});
		window.location.href=_href;
	});


});


if($.validator){
	//设置validate的默认值
	$.validator.setDefaults({
		 submitHandler: function(form) { 
			 layer.load(0,{share:false});
			 form.submit();
		 }
	});

	$.extend($.validator.messages, {
	    required: "必填",
		email: "E-mail格式错误",
		url: "网址格式错误",
		date: "日期格式错误",
		dateISO: "日期格式错误",
		pointcard: "信用卡格式错误",
		number: "只允许输入数字",
		digits: "只允许输入零或正整数",
		minlength: "长度不允许小于{0}",
		maxlength: $.validator.format("长度不允许大于{0}"),
		rangelength: $.validator.format("长度必须在{0}-{1}之间"),
		min: $.validator.format("不允许小于{0}"),
		max: $.validator.format("不允许大于{0}"),
		range: $.validator.format("必须在{0}-{1}之间"),
		accept:  "输入后缀错误",
		equalTo:  "两次输入不一致",
		remote: "输入错误",
		integer:"只允许输入整数",
		positive: "只允许输入正数",
		negative: "只允许输入负数",
		decimal: "数值超出了允许范围",
		pattern: "格式错误",
		extension: "文件格式错误"
	});
}

//模拟点击菜单
function autoMenu(fid,sid,url){
	var nav=$("#menu",window.top.document);
	var left=$("#menuFrame",window.top.document);
	var main=$("#mainFrame",window.top.document);
	nav.find("a.menu").each(function(){
		if($(this).data("id")==fid){
			left.attr("src","/admin/menu/menuTree/"+fid);
			switchNavStyle($(this));
			left.load(function(){
				left.contents().find(".accordion-body a").each(function(){
					if($(this).data("id")==sid){
						main.attr("src",url);
						left[0].contentWindow.switchMenuStyle($(this));
						sid=0;
						return false;
					}
				});
			});
			return false;
		}
	});
}

//切换导航样式
function switchNavStyle(o){
	var nav=$("#menu",window.top.document);
	var left=$("#menuFrame",window.top.document);
	nav.find("li.menu").removeClass("active");
	o.parent().addClass("active");
	if(!left.find("#openClose").hasClass("close")){
		left.find("#openClose").click();
	}
}



;;(function(window,$,undefined){
	var doc = window.document;
	common = {
		init: function(){
			var that = this;
			that._bindEventListener();
		},
		//绑定事件
		_bindEventListener: function(){
			var that = this;
			$(doc)
				.on("click","a[data-action]", function(e){
					var $target = $(this),
					 	action = $.trim($target.attr("data-action")),
					 	operation = that._operation[action];
					if(!$target.hasClass("disabled") && operation){
						operation.call(that,e,$target);
					}
					e.preventDefault();
				});
		},
		_operation: {
			comfirm : function(event,$target){
				var title = $target.attr("data-title") || "确认？",
					_href = $target.attr("href") || "#";
				layer.confirm(title,{
					btn:['确定','取消']
				},function(){
					window.location.href = _href;
				},function(){
					
				});
			}
		}
			
			
	};
	
	window.COMMON = common;
})(this,jQuery);




